﻿using System;

namespace Binary_Tree
{
    class Queue<T>
    {
        private ListNode<T> firstNode;
        private ListNode<T> lastNode;

        public Queue()
        {
            firstNode = lastNode = null;
        }

        public void Enqueue(T data)
        {
            if ( firstNode == null )
                firstNode = lastNode = new ListNode<T>(data);
            else
                lastNode = lastNode.Next = new ListNode<T>(data);
        }

        public T Dequeue()
        {
            if (firstNode == null)
                throw new Exception();

            T value = firstNode.Data;

            if (firstNode == lastNode)
                firstNode = lastNode = null;
            else
                firstNode = firstNode.Next;

            return value;
        }

        public bool IsEmpty()
        {
            return firstNode == null;
        }
    }
}
